package com.zym.community.controller;

import com.zym.community.entity.DiscussPost;
import com.zym.community.entity.Page;
import com.zym.community.service.DiscussPostService;
import com.zym.community.service.LikeService;
import com.zym.community.service.UserService;
import com.zym.community.util.CommunityConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Api("首页接口")
public class HomeController implements CommunityConstant {

    @Autowired
    private UserService userService;

    @Autowired
    private DiscussPostService discussPostService;

    @Autowired
    private LikeService likeService;

    @GetMapping("/index")
    @ApiOperation("分页查询所有帖子")
    public String getIndexPage(Model model, Page page){
        //方法调用前，SpringMVC会自动实例化Model和Page，并将Page注入到Model中
        //所以，在thymeleaf模板中可以直接访问Page对象中的属性
        page.setRows(discussPostService.findDiscussPostRows(0));
        page.setPath("/index");

        List<DiscussPost> posts = discussPostService.findDiscussPosts(0, page.getOffset(), page.getLimit());
        List<Map<String, Object>> list = new ArrayList<>();   //集合存储每个帖子和对应的用户
        if(posts != null){
            for (DiscussPost post : posts) {
                // 把每个帖子和对应的信息放入map
                Map<String, Object> map = new HashMap<>();
                // 帖子
                map.put("post", post);
                // 用户
                map.put("user", userService.findByUserId(post.getUserId()));
                // 点赞数
                long likeCount = likeService.findEntityLikeCount(ENTITY_TYPE_POST, post.getId());
                map.put("likeCount", likeCount);

                list.add(map);   //将map放入集合
            }
        }
        model.addAttribute("discussPosts", list);
        return "index";
    }

    @GetMapping("/search")
    @ApiOperation("模糊搜索")
    public String searchPosts(Model model, Page page, String keyword) {
        List<DiscussPost> posts = discussPostService.searchPosts(keyword, page.getOffset(), page.getLimit());
        List<Map<String, Object>> list = new ArrayList<>();
        if (posts != null){
            for (DiscussPost post : posts) {
                Map<String, Object> map = new HashMap<>();
                map.put("post", post);
                map.put("user", userService.findByUserId(post.getUserId()));
                long likeCount = likeService.findEntityLikeCount(ENTITY_TYPE_POST, post.getId());
                map.put("likeCount", likeCount);

                list.add(map);
            }
        }
        model.addAttribute("discussPosts", list);
        model.addAttribute("keyword", keyword);

        // 分页信息
        page.setPath("/search?keyword=" + keyword);
        page.setRows(discussPostService.searchDiscussPostRows(keyword));
        return "site/search";
    }

    @GetMapping("/error")
    @ApiOperation("错误页面")
    public String getErrorPage(){
        return "error/500";
    }

    @GetMapping("/denied")
    @ApiOperation("拒绝访问的提示页面")
    public String getDeniedPage(){
        return "error/404";
    }
}
